layui.use(["jquery","table","element","layer"],function () {
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var data;
    $.ajaxSettings.async = false;
    $.ajax({
        type: "get",
        url: "/Manage/getParking",
        success: function (obj) {
            data = obj;
        }
    });
    
//    $.ajax({
//        type: "get",
//        url: "/user/getTname",
//        success: function (obj) {
//        	$("#tName").text("欢迎您,"+obj+"老师")
//        }
//    });
    

    let newData = JSON.parse(JSON.stringify(data));

    
    var indexTable = table.render({
        elem: "#indexTable",
        toolbar:"#insert",
        autoSort: false,
        page: true,
        cols: [[
            {field: "id", title: "停车场id",},
            {field: "num", title: "停车场容量",},
            {field: "name", title: "停车场名",},
            {field: "bigCoefficient", title: "大车系数",},
            {field: "newEnergyCoefficient", title: "新能源车系数",},
            {field: "coefficient", title: "基础系数",},
//            {field: "status", title: "状态", templet: function(obj){
//            	console.info(obj);
//            	if(obj.entered === 1){
//            		return "已提交";
//            	}else{
//            		return "未提交";
//            	}
//            } },
            {field: "action", title: "操作", toolbar: "#actions", width: 200},
        ]],
        data: newData
    });
    
    //头工具栏
    table.on("toolbar(indexTable)",function (obj) {
    	console.info(obj);
    	
    	if(obj.event === 'insert'){
    	  layer.open({
		      type: 1,
		      title: '插入停车场系数',
		      maxmin: true,
		      shadeClose: true, //点击遮罩关闭层
		      area : ['360px' , '450px'],
		      content: '<div form class="layui-form" >	<div class="layui-form-item"> <label class="layui-form-label">停车场id</label> <div class="layui-input-block"> <input type="text" required id="id"> </div></div><div class="layui-form-item"> <label class="layui-form-label">停车场容量</label> <div class="layui-input-block"> <input type="text" required id="num"> </div></div> <div class="layui-form-item">  <label class="layui-form-label">停车场名</label><div class="layui-input-block"> <input type="text"  required id="name">	 </div></div> 	     <div class="layui-form-item"> <label class="layui-form-label">大车系数</label> <div class="layui-input-block"> <input type="text" required id="bigCoefficient">	 </div></div> 	  <div class="layui-form-item">  <label class="layui-form-label">新能源车系数</label><div class="layui-input-block">     <input type="text" required id="newEnergyCoefficient">	</div></div>  	     <div class="layui-form-item"> <label class="layui-form-label"> 基础系数</label> <div class="layui-input-block"> <input type="text" required id="coefficient"> </div></div></div>',
		      btn: ['确定','取消'],
		      success: function (layero, index) {	  		    		  		    	
	  		    },
		      yes: function(index, layero){				        
		//		var productNum=document.getElementById("num").value;
		    	let id=document.getElementById("id").value;
  		    	let num=document.getElementById("num").value;
  		    	let name=document.getElementById("name").value;
  		    	let bigCoefficient=document.getElementById("bigCoefficient").value;
  		    	let newEnergyCoefficient=document.getElementById("newEnergyCoefficient").value;
  		    	let coefficient=document.getElementById("coefficient").value;
  		    	let datas={'id':id,'num':num,'name':name, 'bigCoefficient': bigCoefficient ,'newEnergyCoefficient' :newEnergyCoefficient, 'coefficient': coefficient};
				  $.ajax({
	  			        type: "post",
	  			        url: "/Manage/insertParking",
	  			        data:datas,
	  			        success: function (obj) { 	
	  			        	
	  			        	window.location.reload();
	  			        },
				        error:function(){
				        	layer.alert("添加数据失败")
				        }
	  			    });
				  
		      }
		  });
    	};
    });
    
   //行工具
    table.on("tool(indexTable)",function (obj) {
    	 
        let data = obj.data;
   //    $("#num").val();
    //    body.find('#num').val()
     //   console.info(  $("#num").val());
        if(obj.event === 'update'){
      	  layer.open({
  		      type: 1,
  		      title: '修改停车场系数',
  		      maxmin: true,
  		      shadeClose: true, //点击遮罩关闭层
  		      area : ['360px' , '450px'],
  		      content: '<div form class="layui-form" >	<div class="layui-form-item"> <label class="layui-form-label">停车场容量</label> <div class="layui-input-block"> <input type="text" id="num" > </div></div> <div class="layui-form-item">  <label class="layui-form-label">停车场名</label><div class="layui-input-block"> <input type="text" id="name">	 </div></div> 	     <div class="layui-form-item"> <label class="layui-form-label">大车系数</label> <div class="layui-input-block"> <input type="text" id="bigCoefficient">	 </div></div> 	  <div class="layui-form-item">  <label class="layui-form-label">新能源车系数</label><div class="layui-input-block">     <input type="text" id="newEnergyCoefficient">	</div></div>  	     <div class="layui-form-item"> <label class="layui-form-label"> 基础系数</label> <div class="layui-input-block"> <input type="text" id="coefficient"> </div></div></div>',
  		      btn: ['确定','取消'],
  		    success: function (layero, index) {
  		    	 $("#num").val(obj.data.num);
  		    	 $("#name").val(obj.data.name);
  		    	 $("#bigCoefficient").val(obj.data.bigCoefficient);
  		    	 $("#newEnergyCoefficient").val(obj.data.newEnergyCoefficient);
  		    	 $("#coefficient").val(obj.data.coefficient);
  		    	
  		    },
  		      yes: function(index, layero){	
  		    	let id=obj.data.id;
  		    	let num=document.getElementById("num").value;
  		    	let name=document.getElementById("name").value;
  		    	let bigCoefficient=document.getElementById("bigCoefficient").value;
  		    	let newEnergyCoefficient=document.getElementById("newEnergyCoefficient").value;
  		    	let coefficient=document.getElementById("coefficient").value;
  		    	let datas={'id':id,'num':num,'name':name, 'bigCoefficient': bigCoefficient ,'newEnergyCoefficient' :newEnergyCoefficient, 'coefficient': coefficient};
  			   $.ajax({
  			        type: "post",
  			        url: "/Manage/updateParking",
  			        data:datas,
  			        success: function (obj) { 			        	
  			        }
  			    });
  			 window.location.reload();
  		      }
  		  });
      	}else if(obj.event === 'delect'){
      	   $.ajax({
      	        type: "post",
      	        url: "/Manage/delectParking",
      	        data:{'id':obj.data.id},
      	        success: function (obj) {
      	        	window.location.reload();
      	        }
      	    });
      	}

      });
   
});